/**
 * Directive: Inbox <inbox></inbox>
 */
angular.module('EmailApp')
  .directive('inbox', function InboxDrctv () {
    'use strict';

    return {
      restrict: 'EA',
      replace: true,
      scope: true,
      templateUrl: "js/directives/inbox.tmpl.html",
      controllerAs: 'inbox',

      controller: function (InboxFactory, $scope) {
        this.messages = [];
        $scope.$parent.unread = function() {
            var count = 0;
            me.messages.forEach(function(message) {
              if(message.read == false) {
                count++;
              }
            })
            return count;
        };

        this.goToMessage = function (id) {
          InboxFactory.goToMessage(id);
        };
        
        this.deleteMessage = function (id, index) {
          //console.log(id, index)
          InboxFactory.deleteMessage(id, index);
        };

        this.unread = function(id) {
          this.messages.forEach(function(msg) {
            if(msg.id == id) {
              msg.read = false;
            }
          })
        };
        
        //InboxFactory.getMessages()
        //  .then( angular.bind( this, function then() {
        //      this.messages = InboxFactory.messages;
        //    }) );
        var me = this;
        InboxFactory.getMessages(function(data) {
          console.log(data)
          me.messages = data;
        });

      },

      link: function (scope, element, attrs, ctrl) {
        /* 
          by convention we do not $ prefix arguments to the link function
          this is to be explicit that they have a fixed order
        */
      }
    }
  });